package geektime.spring.springbucks;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import geektime.spring.springbucks.mapper.CoffeeMapper;
import geektime.spring.springbucks.mapper.OrderCoffeeMapper;
import geektime.spring.springbucks.mapper.OrderMapper;
import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.model.CoffeeExample;
import geektime.spring.springbucks.model.Order;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
@MapperScan("geektime.spring.springbucks.mapper")
public class SpringBucksApplication implements ApplicationRunner {

    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    private CoffeeMapper coffeeMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderCoffeeMapper orderCoffeeMapper;

    public static void main(String[] args) {
        SpringApplication.run(SpringBucksApplication.class, args);
    }

    private void queryCoffeeByIds() {

        List<Long> ids= Arrays.asList(new Long[]{1L, 2L, 3L});
        CoffeeExample coffeeExample = new CoffeeExample();
        coffeeExample.createCriteria().andIdIn(ids);
        List<Coffee> coffees = coffeeMapper.selectByExample(coffeeExample);
        log.info("coffees is:{}",coffees);
    }

    private void getCoffeeByPage() {
//        参考：https://blog.csdn.net/weixin_44307065/article/details/109473284
        
//        PageHelper
        PageHelper.startPage(1,2);
        CoffeeExample ce=new CoffeeExample();
        List<Coffee> coffees = coffeeMapper.selectByExample(ce);
        log.info("getCoffeeByPage is:{}",coffees);
        PageInfo<Coffee> coffeePageInfo = new PageInfo<>(coffees);
        log.info("page info is:{}",coffeePageInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    private void deleteOrder() {
        int i = orderMapper.deleteByPrimaryKey(1L);
        log.info("影响行数：{}",i);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        generateArtifacts();
//        添加一个订单
        addOrder();
//        查询一个coffee 查询结果缓存到redis
        queryCoffee();
//        删除一个订单
        deleteOrder();
//        分页查询coffee
        getCoffeeByPage();
//        主键批量查询
        queryCoffeeByIds();
//        更新coffee
        updateCoffee();
    }

    @Transactional(rollbackFor = Exception.class)
    private void updateCoffee() {
        Coffee coffee = coffeeMapper.selectByPrimaryKey(1L);
        coffee.setName("name for test");
        coffeeMapper.updateByPrimaryKeySelective(coffee);
        log.info("coffee update to:{}",coffee);
    }

    private void queryCoffee() {
        Long id = 1L;
        Coffee s = coffeeMapper.selectByPrimaryKey(id);
        log.info("Coffee {}", s);
        String k = "coffee_" + id;
        stringRedisTemplate.opsForValue().set(k, String.valueOf(s));
        String s1 = stringRedisTemplate.opsForValue().get(k);
        log.info("Coffee from redis is:{}", s1);
    }

    //
    @Transactional(rollbackFor = Exception.class)
    private void addOrder() {
        Order order = new Order().withCustomer("小张").withCreateTime(new Date()).withUpdateTime(new Date()).withState(1);
        System.out.println("------order is:---------");
        System.out.println(order);
        orderMapper.insertSelective(order);
        System.out.println(order);
    }

    private void generateArtifacts() throws Exception {
        List<String> warnings = new ArrayList<>();
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(this.getClass().getResourceAsStream("/generatorConfig.xml"));
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}

